var express = require('express');
var router = express.Router();
var bkindDAO = require("../bin/db/bigKindDAO.js");
var sKindDAO = require("../bin/db/smallKindDAO.js");
var goodsDAO = require("../bin/db/goodsDAO.js");

var carDAO = require("../bin/db/carDAO.js");


router.get("/lunbo", function (req, res) {
    res.json({
        list: [
            { img: "/imgs/1.jpg" },
            { img: "/imgs/2.jpg" },
            { img: "/imgs/3.jpg" },
            { img: "/imgs/4.jpg" },
            { img: "/imgs/5.jpg" },
            { img: "/imgs/6.jpg" },
            { img: "/imgs/7.jpg" },
        ],
        four: [
            { img: "/imgs/8.jpg" },
            { img: "/imgs/9.jpg" },
            { img: "/imgs/10.jpg" },
            { img: "/imgs/11.jpg" },
        ],
        five: [
            { img: "/imgs/12.jpg" },
            { img: "/imgs/13.jpg" },
            { img: "/imgs/14.jpg" },
            { img: "/imgs/15.jpg" },
            { img: "/imgs/16.jpg" },
        ],
        leibie: [
            { img: "/imgs/17.jpg" },
            { img: "/imgs/18.jpg" },
            { img: "/imgs/19.jpg" },
            { img: "/imgs/20.jpg" }
        ],
        choice: [
            { img: "/imgs/21.jpg" },
            { img: "/imgs/22.jpg" },
            { img: "/imgs/23.jpg" },
            { img: "/imgs/24.jpg" }
        ],
        news: [
            { img: "/imgs/28.jpg" },
            { img: "/imgs/29.jpg" },
            { img: "/imgs/30.jpg" },
            { img: "/imgs/31.jpg" }
        ],
        groom: [
            { img: "/imgs/2.png" },
            { img: "/imgs/1.jpg" },
            { img: "/imgs/2.jpg" },
            { img: "/imgs/3.jpg" },
            { img: "/imgs/4.jpg" },
            { img: "/imgs/6.jpg" },
        ]
    })
})
//请求大分类
router.post("/bigkind", (req, res) => {
    bkindDAO.find()
        .then(data => {
            // console.log(111)
            // console.log(data)
            res.json({ err: 0, data })
        })
})
//请求一个大分类的数据
router.post("/bigkinditem", (req, res) => {
    bkindDAO.findOne({ bigkind: req.body.bigkind })
        .populate("contain")
        .then(data => {
            // console.log(data);
            res.json({ data });
        });
});

// 请求所有的商品
router.get("/goodsAll", (req, res) => {
    goodsDAO.find()
        .then(data => {
            console.log(1131)
            // console.log(data)
            res.json({ err: 0, data })
        })
})
// 根据小类名称查找某个小类的所有商品
router.get("/itemskindAll", (req, res) => {
    console.log(req.query)
     var greg = new RegExp(req.query.smallkind);
    var condition = {};
    if (req.query.asc) {
        if (req.query.asc == 1) {
            condition.price = 1;
        } else {
            condition.price = -1;
        }
    }
    if(req.query.asc!=1){
        condition.sales = -1;
        goodsDAO.find({smallkind:greg})
        .sort(condition)
        .then(data=>{
            res.json({err:0,data:data});
        });
    } 
    if(req.query.select){
        if(req.query.lowprice&&req.query.highprice){
            goodsDAO.find({smallkind:greg,price:{$gte:req.query.lowprice*1,$lte:req.query.highprice*1}})
            .then(data=>{
                res.json({err:0,data:data});
            });
        }else if(req.query.lowprice*1&&!req.query.highprice*1){
            goodsDAO.find({smallkind:greg,price:{$gte:req.query.lowprice*1}})
            .then(data=>{
                res.json({err:0,data:data});
            });
        }else if(!req.query.lowprice&&req.query.highprice){
           goodsDAO.find({smallkind:greg,price:{$lte:req.query.highprice*1}})
            .then(data=>{
                res.json({err:0,data:data});
            });
        }else{
        goodsDAO.find({ smallkind: greg })
        .sort(condition)
        .then(data => {
            // console.log(data);
            res.json({ err: 0, data: data });
        });
    }
}
    
});

//根据id获取商品详情
router.post("/skindItem", (req, res) => {
    goodsDAO.findOne({ _id: req.body._id })
        .then(data => {
            var list = [];
            for (var i = 0; i < data.pics.length; i++) {
                list.push({
                    img: '/imgs/goods/' + data.pics[i]
                });
            }
            data.pics = list;
            // console.log(data);
            res.json({ err: 0, data: data });
        });
});

// 添加编辑商品规格
router.post("/addregGoods", (req, res) => {
    goodsDAO.findOne({ _id: req.body._id })
        .then(data => {
            data.reg = req.body.reg;
            data.save()
                .then(data1 => {
                    res.json({ err: 0, msg: "添加成功" });
                });
        });
});


//根据关键字查询渲染商品列表的数据 
router.post("/search", (req, res) => {
    var reg = new RegExp(req.body.txt);
    goodsDAO.find({ goodsname: reg })
        .then(data => {
            res.json({ err: 0, data: data });
        });
})

//用户添加购物车
router.post("/chartadd", (req, res) => {
    console.log(req.body)
    carDAO.findOne({users:req.body.users,goods:req.body.goods,reg:req.body.reg})
    .then(data=>{
        if(data){
            data.gnum = data.gnum*1+req.body.gnum*1;
            data.save();
            res.json({err:0, msg: "加入购物车成功"})
        }else{
             var c = new carDAO(req.body);
             c.save()
            .then(data => {
                console.log(data)
    
                res.json({ err: 0, msg: "加入购物车成功" });
            });
        }
    })
   
})
// 用户的购物车
router.post("/findchart", (req, res) => {
    carDAO.find({ users: req.body._id })
        .populate("goods")
        .then(data => {
            res.json({ err: 0, data: data });
        });
});
// 编辑购物车
router.post("/editchart", (req, res) => {
    var currentObj = {};
    if (req.body.gnum) {
        currentObj.gnum = req.body.gnum;
    }
    if (req.body.reg) {
        currentObj.reg = req.body.reg;
    }
    carDAO.update({ _id: req.body._id }, {
        $set: currentObj
    })
        .then(data => {
            res.json({ err: 0, msg: "商品数据修改成功" });
        });
});
// 删除
router.post("/deletechart", (req, res) => {
    var arr = req.body.inputId;
    var parr = [];
    for (var i = 0; i < arr.length; i++) {
        parr.push(carDAO.remove({ _id: arr[i] }));
    }
    Promise.all(parr)
        .then(data => {
            res.json({ err: 0, msg: '删除成功' });
        });
});
module.exports = router;
